package com.yx.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.modules.api.entity.TbVolume;
import com.yx.modules.api.service.TbVolumeService;
import com.yx.system.entity.TdBook;
import com.yx.system.entity.TdSection;
import com.yx.system.service.TdBookService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 卷
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-06-13 11:26:00
 */
@RestController
@RequestMapping("/sys/volume")
public class TbVolumeController2 {
	@Autowired
	private TbVolumeService volumeService;

	@Autowired
	private TdBookService tdBookService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:volume:list")
	public R list(@RequestParam Map<String, Object> map){

//		Query query = new Query(map);
//		Page<TbVolume> pageUtil = new Page<TbVolume>(query.getPage(), query.getLimit());
//		Page<TbVolume> page = volumeService.queryPageList(pageUtil, query);
//		return R.ok().put("page", page);

		//查询列表数据
        Query query = new Query(map);

		List<TbVolume> volumeList = volumeService.queryList(query);
		int total = volumeService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(volumeList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/findById/{id}")
	@RequiresPermissions("sys:volume:findById")
	public R info(@PathVariable("id") Integer id){
		TbVolume volume = volumeService.queryObject(id);

		return R.ok().put("volume", volume);
	}


	@RequestMapping("/findByBookId/{id}")
	@RequiresPermissions("sys:volume:findByBookId")
	public R findByBookId(@PathVariable("id") Integer id){
		List<TbVolume> tbVolumes = volumeService.findByBookId(id);


		return R.ok().put("list", tbVolumes);
	}
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:volume:save")
	public R save(String volumeName,Integer bookid){
		if (volumeName.equals("null")){
			return R.error("卷名不能为空");
		}
		//查询图书所有的卷
		List<TbVolume> tbVolumes = volumeService.findByBookId(bookid);
		//循环遍历判断名称是否相同
		for (TbVolume tbVolume:tbVolumes){
			if (tbVolume.getVolumeName().equals(volumeName)){
				return R.error("卷名称已经存在了");
			}
		}
		TbVolume tbVolume = new TbVolume();
		//查询书籍
		TdBook tdBook = tdBookService.selectByPrimaryKey(bookid);
		if (tdBook==null){
			return R.error("书籍不存在");
		}
		tbVolume.setBookName(tdBook.getBookname());
		tbVolume.setVolumeName(volumeName);
		tbVolume.setBookId(tdBook.getId());
		tbVolume.setCreateDate(new Date());
		tbVolume.setUpdateDate(new Date());
		volumeService.save(tbVolume);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:volume:update")
	public R update(String id,String volumeName){
		if (volumeName.equals("null")){
			return R.error("卷名称不能为空");
		}
		TbVolume tbVolume = volumeService.queryObject(Integer.valueOf(id));
		if (tbVolume==null){
			return R.error("卷不存在");
		}
		tbVolume.setVolumeName(volumeName);
		tbVolume.setUpdateDate(new Date());
		volumeService.update(tbVolume);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:volume:delete")
	public R delete(@RequestBody Integer[] ids){
		volumeService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
